<template>
  <div class="problem_type_count">
    <one-title title="现场问题类型统计" @refreshData="refresh"></one-title>
    <div class="fixed_top_56">
      <div class="multiple_search">
        <div class="search_item">
          <div class="select_label">
            选择工序：
          </div>
          <div class="search_wrapper">
            <el-select
              v-model="workstageIds"
              multiple
              collapse-tags
              placeholder="请选择工序"
              filterable
              size="small">
              <el-option
                v-for="(item, index) in workStageList"
                :key="index"
                :label="item.NAME"
                :value="item.ID">
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="search_item">
          <span class="search_label">问题创建日期：</span>
          <el-date-picker
            v-model="completionDate"
            type="daterange"
            size="small"
            align="right"
            unlink-panels
            :clearable=false
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            :picker-options="pickerOptions">
          </el-date-picker>
        </div>
        <div class="mes_btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            class="search_btn"
            @click="init">
          </el-button>
        </div>
      </div>
      <div class="chart_box">
        <div ref="myChart" class="chart_wrapper" v-loading="isLoading"></div>
      </div>
      <div class="table_container">
        <el-table
          :data="listData"
          ref="tableNode"
          max-height="440"
          v-loading="isLoading"
          border
          stripe
          @sort-change="_sort"
          tooltip-effect="dark">
          <el-table-column
            label="工序名称"
            width="200">
            <template slot-scope="scope">
              {{scope.row.workstageName + '/' + scope.row.workstageNumber}}
            </template>
          </el-table-column>
          <el-table-column
            label="问题类型">
            <mes-column :key="index" v-for="(column, index) in tableColumns"
                        :column="column">
            </mes-column>
          </el-table-column>
          <el-table-column
            label="问题总数"
            prop="count"
            sortable="custom"
            align="center"
            :resizable="false"
            min-width="60">
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import tableColumns from './tableColumns'

export default {
  name: 'ProblemTypeCount',
  data() {
    return {
      tableColumns: tableColumns, // table列配置
      isLoading: false, // 加载状态
      sortData: {}, // 排序对象
      workstageIds: [], // 工序
      problemCount: [],
      completionDate: [
        this.moment(window.serverTime).subtract(1, 'months').format('YYYY-MM-DD 00:00:00'),
        this.moment(window.serverTime).format('YYYY-MM-DD 23:59:59')
      ], // 工序完成日期（默认当前日期往前推算一个月）
      workStage: [], // x轴
      deviceException: [], // 设备异常
      materialException: [], // 物料异常
      technologyException: [], // 技术异常
      designException: [], // 设计异常
      quanlityException: [], // 质量异常
      toolException: [], // 工具异常
      others: [], // 其他异常
      pickerOptions: this.$dateConfig.pickerOptions // 日期格式的快捷键配置项
    }
  },
  computed: {
    // 工序
    workStageList() {
      return this.$store.state.problemTypeCount.workStageList
    },
    // 图表数据
    listData() {
      return this.$store.state.problemTypeCount.listData || []
    }
  },
  mounted() {
    this.getWorkStage()
    this.init()
  },
  methods: {
    // 默认选中全部工序
    selectedAll() {
      this.workstageIds = this.workStageList.reduce((pre, cur) => [...pre, cur.ID], [])
    },
    // 刷新
    refresh() {
      this.completionDate = [
        this.moment(window.serverTime).subtract(1, 'months').format('YYYY-MM-DD 00:00:00'),
        this.moment(window.serverTime).format('YYYY-MM-DD 23:59:59')
      ]
      this.init()
      this.selectedAll()
    },
    // 初始化
    init() {
      this.isLoading = true
      let postData = {
        method: 'post',
        url: 'a/turnoverworkstage/turnoverWorkstage/fieldproblemtypestatistics',
        data: {},
        module: 'problemTypeCount',
        state: 'listData'
      }
      if (this.workstageIds.length) {
        postData.data.workstageIds = this.workstageIds.join(',')
      }
      if (this.completionDate.length > 0) {
        postData.data.beginWorkStageDate = this.completionDate[0]
        postData.data.endWorkStageDate = this.completionDate[1]
      }
      if (Object.keys(this.sortData).length > 0) {
        postData.data.sort = this.sortData
      }
      this.$store.dispatch('saveData', postData).then(res => {
        // 初始化图表数据
        this.echartsData()
        // 设置图表滚动
        let dataZoom = [
          {
            // 滚动条
            type: 'slider',
            show: false,
            showDetail: false,
            start: 0,
            end: 100,
            bottom: 10,
            height: 16,
            handleStyle: {
              /*手柄的样式*/
              color: '#fff',
              borderWidth: 2,
              borderColor: '#a1b1c1'
            },
          }
        ]
        let max = 12
        if (res.length >= max) {
          let end = Math.floor(max / res.length * 100)
          dataZoom.forEach(item => {
            item.end = end
            item.show = true
          })
        }
        this.drawLine(dataZoom)
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 获取工序列表
    getWorkStage() {
      this.$store.dispatch('saveData', {
        method: 'get',
        url: 'a/workstagerepository/workstageRepository/listfields/id,name',
        module: 'problemTypeCount',
        state: 'workStageList'
      }).then(() => {
        this.selectedAll()
      })
    },
    _sort(obj) {
      this.sortData = this.$getSortOrder(obj)
      this.init()
    },
    // 图表所需数据
    echartsData() {
      this.workStage = [] // x轴
      this.deviceException = [] // 设备异常
      this.materialException = [] // 物料异常
      this.technologyException = [] // 技术异常
      this.designException = [] // 设计异常
      this.quanlityException = [] // 质量异常
      this.toolException = [] // 工具异常
      this.others = [] // 其他异常
      this.listData.forEach(item => {
        this.workStage.push(item.workstageName + '/' + item.workstageNumber || '')
        this.deviceException.push(item.deviceException || 0)
        this.materialException.push(item.materialException || 0)
        this.technologyException.push(item.technologyException || 0)
        this.designException.push(item.designException || 0)
        this.quanlityException.push(item.quanlityException || 0)
        this.toolException.push(item.toolException || 0)
        this.others.push(item.others || 0)
      })
    },
    // 绘制图表
    drawLine(dataZoom) {
      // 初始化echarts实例
      let myChart = this.$echarts.init(this.$refs.myChart)
      myChart.setOption({
        // 弹窗样式
        tooltip: {
          trigger: 'axis',
          axisPointer: {type: 'shadow'},
          padding: 10,
          textStyle: {fontSize: 12},
          backgroundColor: 'rgba(0, 0, 0, 0.85)',
        },
        // 缩放,滚动
        dataZoom: dataZoom,
        // 画板边距
        grid: {
          left: '60',
          right: '60'
        },
        legend: {
          itemWidth: 24,
          itemHeight: 10,
          data: [
            {name: '设备异常'},
            {name: '物料异常'},
            {name: '技术异常'},
            {name: '设计异常'},
            {name: '质量异常'},
            {name: '工具异常'},
            {name: '其他异常'}
          ]
        },
        xAxis: {
          type: 'category',
          data: this.workStage,
          // x轴轴线颜色和宽度
          axisLine: {
            lineStyle: {
              color: '#dcdfe6',
              width: 2
            }
          },
          // x轴字体颜色
          axisLabel: {
            show: true,
            textStyle: {
              color: 'rgba(0, 0, 0, 0.65)'
            }
          }
        },
        yAxis: [
          {
            type: 'value',
            name: '次数',
            // Y轴虚线
            splitLine: {
              lineStyle: {
                color: 'rgba(0, 0, 0, 0.1)',
                width: 1,
                type: 'dashed'
              }
            },
            // y轴轴线
            axisLine: {'show': false},
            //y轴刻度线
            axisTick: {'show': false},
          }
        ],
        // 数据
        series: [
          {
            name: '设备异常',
            type: 'bar',
            stack: '问题类型',
            data: this.deviceException,
            barMaxWidth: 160,
            itemStyle: {
              normal: {
                color: '#1890ff',
                opacity: 0.85
              }
            },
          },
          {
            name: '物料异常',
            type: 'bar',
            stack: '问题类型',
            data: this.materialException,
            barMaxWidth: 160,
            itemStyle: {
              normal: {
                color: '#37cccc',
                opacity: 0.85
              }
            }
          },
          {
            name: '技术异常',
            type: 'bar',
            stack: '问题类型',
            data: this.technologyException,
            barMaxWidth: 160,
            itemStyle: {
              normal: {
                color: '#4ecc74',
                opacity: 0.85
              }
            }
          },
          {
            name: '设计异常',
            type: 'bar',
            stack: '问题类型',
            data: this.designException,
            barMaxWidth: 160,
            itemStyle: {
              normal: {
                color: '#fbd438',
                opacity: 0.85
              }
            }
          },
          {
            name: '质量异常',
            type: 'bar',
            stack: '问题类型',
            data: this.quanlityException,
            barMaxWidth: 160,
            itemStyle: {
              normal: {
                color: '#f04864',
                opacity: 0.85
              }
            }
          },
          {
            name: '工具异常',
            type: 'bar',
            stack: '问题类型',
            data: this.toolException,
            barMaxWidth: 160,
            itemStyle: {
              normal: {
                color: '#9860e5',
                opacity: 0.85
              }
            }
          },
          {
            name: '其他异常',
            type: 'bar',
            stack: '问题类型',
            data: this.others,
            barMaxWidth: 160,
            itemStyle: {
              normal: {
                color: '#5354d0',
                opacity: 0.85
              }
            }
          }
        ]
      })
    }
  }
}
</script>
<style scoped lang="less">
  .problem_type_count {
    .chart_box {
      padding: 0 20px;
      .chart_wrapper {
        width: 100%;
        height: 500px;
        box-sizing: border-box;
        border: 1px solid #dcdfe6;
        margin-bottom: 20px;
        padding-top: 20px;
      }
    }
    .table_container{
      padding-bottom: 40px;
    }
  }
</style>
